package test;

public class JinSan {
	private static int count;
	static int i;
	private static Object lock = new Object();

	static class Threads implements Runnable {
		int id;

		public Threads(int id) {
			this.id = id;
		}

		@Override
		public void run() {
			for (;;) {
				synchronized (lock) {
					while (id != count % 4) {

						try {
							lock.wait();
						} catch (InterruptedException e) {

						}

					}
					if (id == 0 || id == 1) {
						System.out.println(Thread.currentThread().getName() + " " + i++ + " ");
					}
					if (id == 2 || id == 3)
						System.out.println(Thread.currentThread().getName() + " " + i-- + " ");
					count++;
					lock.notifyAll();

				}

			}
		}
	}

	public static void main(String args[]) {
		Thread a = new Thread(new Threads(0), "a");
		Thread b = new Thread(new Threads(1), "b");
		Thread c = new Thread(new Threads(2), "c");
		Thread d = new Thread(new Threads(3), "d");
		a.start();
		b.start();
		c.start();
		d.start();
	}
}
